package command.client;

import client.ClientSession;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.MessageCommunication;
import model.User;

public class CreateUserClient implements CommandClient {

    private User userToCreate;
    private MessageCommunication operationStatus;

    public CreateUserClient() {
    }

    public void setUser(User user) {
        userToCreate = user;
    }

    public MessageCommunication getOperationStatus() {
        return operationStatus;
    }

    @Override
    public void execute(ClientSession clientSession, boolean isInvokedByTheServer) {
        try {
            clientSession.getObjectOutputStream().writeObject(new MessageCommunication(null, "createuserserver"));
            clientSession.getObjectOutputStream().flush();
            clientSession.getObjectOutputStream().writeObject(userToCreate);
            clientSession.getObjectOutputStream().flush();
            Object tmp = clientSession.getObjectInputStream().readObject();
            if (tmp != null) {
                operationStatus = (MessageCommunication) tmp;
            }
        } catch (ClassNotFoundException | IOException ex) {
            Logger.getLogger(CreateUserClient.class.getName()).log(Level.SEVERE,
                    "Hubo un problema a la hora de enviar los datos del nuevo Cliente", ex);
        }
    }
}
